<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/5/8
 * Time: 14:18
 */

namespace frontend\controllers;
use Yii;
use common\models\User;
use frontend\models\Address;


class AddressController extends CommonController
{
    public function actionAdd()
    {
        if (!Yii::$app->session['user']) {
            return $this->redirect(['member/auth']);
        }
        $userid = Yii::$app->session['user']['uid'];
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            $post['userid'] = $userid;
            $post['address'] = $post['address1'].$post['address2'];
            $data['Address'] = $post;
            $model = new Address;
            $model->load($data);
//            var_dump($model);die;
            $model->save();

        }

        return $this->redirect($_SERVER['HTTP_REFERER']);
    }

    public function actionDel()
    {
        if (!Yii::$app->session['user']) {
            return $this->redirect(['member/auth']);
        }
        $userid = Yii::$app->session['user']['uid'];
        $addressid = Yii::$app->request->get('addressid');
        if (!Address::find()->where('userid = :uid and addressid = :aid', [':uid' => $userid, ':aid' => $addressid])->one()) {
            return $this->redirect($_SERVER['HTTP_REFERER']);
        }
        Address::deleteAll('addressid = :aid', [':aid' => $addressid]);
        return $this->redirect($_SERVER['HTTP_REFERER']);
    }
}